package Tcp;
import cn.hutool.json.JSONUtil;
import entity.Message;
import handle.MyClientHandler;
import handle.TcpClientHandle;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import redis.clients.jedis.Jedis;
import utils.RedisPoolUtil;

public class MyTcpClient1 {

    public static void main(String[] args) throws Exception
    {

        //只要一个线程组
        EventLoopGroup group=new NioEventLoopGroup();
        try{
            //启动实例是bootstrap
            Bootstrap b=new Bootstrap();
            b.group(group)
                    //连接类型是NioSocketChannel.class
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(new StringDecoder());
                            sc.pipeline().addLast(new StringEncoder());
                            sc.pipeline().addLast(new TcpClientHandle());
                        }
                    });
            Message msg=new Message("你好我是1","2");
            String s=JSONUtil.toJsonStr(msg);
            Channel ch=b.connect("127.0.0.1",8080).sync().channel();
            ch.writeAndFlush(s).sync();
            ch.closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }

}